/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.otherUtil;

import com.entity.Event;
import delete.Forecast;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 *
 * @author jackie
 */
@Singleton
public class schedule {
    @EJB
    private com.ejb.EventFacade ejbFacade;
    @PersistenceContext
    private EntityManager em;
    
    @Schedule(hour="*",minute="*/30",second="*")
    public void checkingWeather() throws ParseException{
 //       System.out.println("abc"+new Date());
        
        Forecast f = new Forecast();
        
        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果           
        System.out.println(date);
//        Calendar cal = Calendar.getInstance();
//       cal.add(Calendar.DATE, 1);
       SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
       String date1 = format1.format(date); 
       Date inActiveDate = format1.parse(date1);
//        System.out.println(inActiveDate);
        List<Event> event = em.createNamedQuery("Event.findAllByDateOrderByEventDate",Event.class).setParameter("eventDate", inActiveDate).getResultList();
        for(int i=0; i<event.size(); i++)
        {   
////           Date eventDate = event.get(i).getEventDate();
////            if(eventDate.getDate()==date.getDate() && eventDate.getMonth()==date.getMonth() && eventDate.getYear()==date.getYear() ){
////                event.get(i).setEventNotification(f.checkingWeather(event.get(i).getEventCity()));
////                ejbFacade.edit(event.get(i));
////            }
             event.get(i).setEventNotification(f.checkingWeather(event.get(i).getEventCity()));
             ejbFacade.edit(event.get(i));
        }
    }
    
    //test
//     public static void main(String[] args) throws ParseException
//    {
//	Forecast f = new Forecast();
//	System.out.println(f.checkingWeather("milano"));
        
//        Date date=new Date();//取时间
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(date);
//        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
//        System.out.println(date);
//        date=calendar.getTime(); //这个时间就是日期往后推一天的结果        
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        String dateString = formatter.format(date);
//          
//        System.out.println(dateString);
//        
//        
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
//        String date1 = format1.format(date);            
//        Date inActiveDate = null;
//        try {
//            inActiveDate = format1.parse(date1);
//        } catch (ParseException e1) {
//            // TODO Auto-generated catch block
//            e1.printStackTrace();
//        }
//
//        System.out.println(date);
//        
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.DATE, 1);
//        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(cal.getTime());
//        String formatted = format2.format(cal.getTime());
//        System.out.println(formatted);// Output "2012-09-26"
//
//        System.out.println(format2.parse(formatted));
        // Output "Wed Sep 26 00:00:00 EST 2012"
//        
//   }
    
}
